//初始化界面禁用进度条


$(function(){
	$(".content_list").mCustomScrollbar()
	// $(".song_geci_container").mCustomScrollbar()
	var audio=$("audio")
	var player =new Player(audio)
	var voice_jindu
	var music_jindu
	var geci

	
	getlist()
	function getlist(){
		$.ajax({
			url:"source/musiclist.json",
			dateType:"json",
			success:function(data){
				 player.musicList=data;
				 console.log(player.musicList)
				 var list=$(".content_list ul")
				$.each(data,function(index,ele){
					var creat=new song(index,ele)
					list.append(creat)
				})
				intmusic(data[0]);
				intgeci(data[0]);
				audio.attr("src",data[0].link_url)
			},
			error:function(reason){
				console.log(reason);
			}
			
		})
	}
	
	function intmusic(music){
		var image=$(".song_pic img,.song_pic_page img")
		// var image2=$(".song_pic_page img")
		var song_name=$(".song_name a")
		var song_singer=$(".song_singer a")
		var song_album=$(".song_album a")
		var jindu_name=$(".jindu_name")
		var jindu_time=$(".jindu_time")
		var mask_bg=$(".mask_bg")
		image.attr("src",music.cover)
		song_name.text(music.name)
		// console.log(music.name)
		song_singer.text(music.singer)
		song_album.text(music.album)
		jindu_name.text(music.name+"/"+music.singer)
		jindu_time.text("00:00/"+music.time)
		mask_bg.css("background","url('"+music.cover+"')"+"no-repeat").css("background-size","cover")
	}
	
	function intgeci(music){
		geci=Geci(music.link_lrc)
		var list=$(".song_geci")
		var list_pure=$(".song_geci_pure")
		especial_index=[]		//当歌词大于一行
		especial_size=[]
		especial_index_pure=[]		//当歌词大于一行
		especial_size_pure=[]
		geci.loadgeci(function(){
			$.each(geci.ci_ku,function(index,ele){
				var list_geci=$("<li>"+ele+"</li>")
				var list_geci_pure=$("<li>"+ele+"</li>")
				list.append(list_geci)
				if(list_geci.height()>40){
					especial_index.push(index)
					especial_size.push(list_geci.height())
				}
				list_pure.append(list_geci_pure)
				if(list_geci_pure.height()>70){
					especial_index_pure.push(index)
					especial_size_pure.push(list_geci.height())
				}
			})
		})
	}
	
	jindutiao()
	function jindutiao(){
		var jindu_tiao=$(".jindu_tiao")
		var jindu_dian=$(".jindu_dian")
		var jindu_bottom=$(".jindu_bottom")
		music_jindu=Jindu(jindu_bottom,jindu_tiao,jindu_dian)
		music_jindu.jindu_click(function(rate){
			player.music_skip(rate)
		})
		music_jindu.jindu_drag("jindu",function(rate){
			player.music_skip(rate)
		})
		move=0
		move_pure=0
		player.time_update(function(all_time,now_time){
			music_jindu.jindu_tiaoupdate(all_time,now_time)
			var index=geci.index_mate(now_time)
			console.log('i',index);
			var now_list=$(".song_geci li").eq(index)
			now_list.addClass("xuan")
			now_list.siblings().removeClass("xuan")
			var now_list_pure=$(".song_geci_pure li").eq(index)
			now_list_pure.addClass("xuan")
			now_list_pure.siblings().removeClass("xuan")
			move=(index-2)*40		//纯净模式下需要重构40
			move_pure=(index-3)*70
			for(var i=0;i<especial_index.length;i++){
				if(especial_index[i]>=0&&especial_index[i]<=index){
					move+=especial_size[i]-40
					move_pure+=especial_size[i]-70
				}
			}
			if(index>=3){
				$(".song_geci").animate({marginTop:-move},200)
			}
			else $(".song_geci").animate({marginTop:0},200)	//防止进度条时间跳转至第三行词之前无法跳转
			if(index>=4){
				$(".song_geci_pure").animate({marginTop:-move_pure},200)
			}
			else $(".song_geci_pure").animate({marginTop:0},200)
		})
		var voice_tiao=$(".voice_tiao")
		var voice_dian=$(".voice_dian")
		var voice_bottom=$(".voice_bottom")
		voice_jindu=Jindu(voice_bottom,voice_tiao,voice_dian)
		voice_jindu.jindu_click(function(rate){
			player.voice_click(rate)
		})
		voice_jindu.jindu_drag("voice",function(rate){
			player.voice_click(rate)
		})
	}
	
	initevents()
	function initevents(){
		$(".list_music").hover(function(){
			$(this).css("opacity",1)
		},
		function(){
			$(this).css("opacity",0.8)
		})
		$("body").delegate(".list_music","mouseenter",function(){		//jq事件委托
			$(this).find(".list_time span").stop().fadeOut(1000);	//时长
			$(this).find(".list_menu").stop().fadeIn(1000);			//播放等按钮
			$(this).find(".list_time a").stop().fadeIn(1000);		//删除用的图标
		})
		$("body").delegate(".list_music","mouseleave",function(){
			$(this).find(".list_menu").stop().fadeOut(1000);
			$(this).find(".list_time a").stop().fadeOut(1000);
			$(this).find(".list_time span").stop().fadeIn(1000);
		})
		$("body").delegate(".list_check","click",function(){
			$(this).toggleClass("checked")
		})
		
		$(".list_title .list_check").click(function(){
			$(".list_check").toggleClass("checked")
			$(this).toggleClass("checked")
		})
		
		$(".music_love").click(function(){
			$(".music_love").toggleClass("love")
		})

		$("body").delegate(".content_button_del","click",function(){	//上方的删除按钮
			$(".list_check.checked").siblings(".list_time").find("a").trigger("click")
		})
		$("body").delegate(".content_button_alldel","click",function(){		//上方的清空按钮
			$(".list_check").siblings(".list_time").find("a").trigger("click")
		})

		$("body").delegate(".list_menu_play","click",function(){
			var music_now=$(this).parents(".list_music")
			$(this).toggleClass("list_menu_played")
			music_now.siblings().find(".list_menu_play").removeClass("list_menu_played")
			if($(this).attr("class").indexOf("list_menu_played")==-1){		//由于上上行代码的影响，此处与预期相反
				$(".music_relay").removeClass("music_relayed")
				music_now.find("div").css("color","rgba(255,255,255,0.5)")
				music_now.find(".list_index").removeClass("list_index_xuan")
			}
			else{
				$(".music_relay").addClass("music_relayed")
				music_now.find("div").css("color","rgba(255,255,255,1)")
				music_now.siblings().find("div").css("color","rgba(255,255,255,0.5)")
				music_now.find(".list_index").addClass("list_index_xuan")
				music_now.siblings().find(".list_index").removeClass("list_index_xuan")
				if(music_now.get(0).index!=player.nowindex){
					$(".mask_bg").fadeOut(1000,function(){
						intmusic(music_now.get(0).music)
						$(".song_geci,.song_geci_pure").html("").css("marginTop",0)
						intgeci(music_now.get(0).music)
						$(".mask_bg").fadeIn(1000)
					})
				}
			}
			player.playmusic(music_now.get(0).index,music_now.get(0).music)
		})
		
		
		geci_drag(".song_geci_container",".song_geci")
		geci_drag(".song_geci_container_pure",".song_geci_pure")
		function geci_drag(selector,move_content){
			$("body").delegate(selector,"mousedown",function(){
				var click_top=event.pageY
				var base_top=parseInt($(move_content).css("margin-top"))
				$(document).mousemove(function(event){
					var move=event.pageY-click_top+base_top
					var is_move=1
					if(move>0)
						move=0
					if(move<parseInt($(move_content).css("height"))*-1+parseInt($(move_content+" li:last").css("height")))
						move=parseInt($(move_content).css("height"))*-1+parseInt($(move_content+" li:last").css("height"))
					$(move_content).css("margin-top",move)
					$(document).mouseup(function(){
						$(document).off("mousemove")
						is_move=0
						base_top+=move
					})
				})
			})
		}
		
		$('.song_geci_container').mousewheel(function(event, delta, deltaX, deltaY) {
				    if (window.console && console.log) {
				         console.log(delta, deltaX, deltaY);
				    }
				});
		
		$(".user_check li:eq(0)").click(function(){
			$(".black_mask").fadeIn(1000)
			$(".login_page").fadeIn(1000).css("display","flex")
		})
		
		$(".user_check li:eq(1)").click(function(){
			$(".black_mask").fadeIn(1000)
			$(".register_page").fadeIn(1000).css("display","flex")
		})
		
		$("body").delegate(".login_exit","click",function(){
			$(".black_mask").fadeOut(1000)
			$(".login_page").fadeOut(1000)
			$(".register_page").fadeOut(1000)
		})
		
		$("body").delegate(".tel_a,.psw_a,.email_a","click",function(){
			$(this).addClass("login_checked")
			$(this).siblings().removeClass("login_checked")
		})
		
		
		
		$("body").delegate(".login_page .tel_a","click",function(){
			var login_form=$(
				"<div class=\"input\">\n"+
					"<label class=\"iconfont icon-shoujihao bolder tel_icon\" ></label>\n"+
					"<input type=\"text\" placeholder=\"电话号码\" name=\"tel\" class=\"tel\">\n"+
				"</div>\n"+
				"<div class=\"input\">\n"+
					"<label class=\"iconfont icon-yanzhengma bolder yanzhengma_icon\" ></label>\n"+
					"<input type=\"message\" placeholder=\"短信验证码\" name=\"message\" class=\"message\">\n"+
					"<button class=\"message_button\">获取验证码</button>\n"+
				"</div>\n"+
				"<div>\n"+
					"<button class=\"login_button\">登录</button>\n"+
				"</div>")
			$('.login_content').html("").html(login_form)
		})
		
		$("body").delegate(".login_page .psw_a","click",function(){
			var login_form=$(
				"<div class=\"input\">\n"+
					"<label class=\"iconfont icon-denglu bolder username_icon\" ></label>\n"+
					"<input type=\"text\" placeholder=\"邮箱或电话\" name=\"username\" class=\"username\">\n"+
				"</div>\n"+
				"<div class=\"input\">\n"+
					"<label class=\"iconfont icon-denglu-mima bolder password_icon\" ></label>\n"+
					"<input type=\"password\" placeholder=\"密码\" name=\"password\" class=\"password\">\n"+
				"</div>\n"+
				"<div>\n"+
					"<button class=\"login_button\">登录</button>\n"+
				"</div>")
			$('.login_content').html("").html(login_form)
		})
		
		$("body").delegate(".register_page .email_a","click",function(){
			console.log('find',$(".register_content .input").eq(0).find("label").get(0));
			var login_form=$(
					'<label class="iconfont icon-youxiang bold register_email_icon" for="email"></label>\n'+
					'<input type="text" placeholder="邮箱" name="email" id="email" class="username">')
			$(".register_content .input").eq(0).html("").html(login_form)
		})
		
		$("body").delegate(".register_page .tel_a","click",function(){
			console.log('find',$(".register_content .input").eq(0).find("label").get(0));
			var login_form=$(
					'<label class="iconfont icon-shoujihao bold register_tel_icon" for="tel"></label>\n'+
					'<input type="text" placeholder="电话" name="tel" id="tel" class="username">')
			$(".register_content .input").eq(0).html("").html(login_form)
		})
		
		 
		 $(".music_pure").click(function(){
			 if($(".music_pure").attr("class").indexOf(" pure")==-1){
				 $(".content").fadeOut(2000)
				 $(".pure_mode_page").fadeIn(2000)
			 }
			 else{
				 $(".content").fadeIn(2000)
				 $(".pure_mode_page").fadeOut(2000)
			 }
			 $(".music_pure").toggleClass("pure")
		 })
		
		$(".song_pic img:eq(0)").click(function(){
			$(".song_pic_page").fadeIn(1000)
		})
		
		$(".song_pic_page").click(function(){
			$(".song_pic_page").fadeOut(1000)
		})
		
	}
	
	$(".music_relay").click(function(){
		if(player.nowindex==-1){
			$(".list_music").eq(0).find(".list_menu_play").trigger("click")
		}
		else{
			$(".list_music").eq(player.nowindex).find(".list_menu_play").trigger("click")
		}
	})
	
	$(".music_befo").click(function(){
		if($(".music_mode").attr("class").indexOf("mode3")!=-1||	//单曲循环（其实应该放在player
		($(".music_mode").attr("class").indexOf("mode1")!=-1&&	//顺序播放
		player.nowindex==0)
		){		
			player.audio.currentTime=0
			player.audio.play();
			$(".song_geci").animate({marginTop:0},200)
		}
		else{
			$(".list_music").eq(player.preindex()).find(".list_menu_play").trigger("click")
		}
		
	})
	$(".music_next").click(function(){
		if($(".music_mode").attr("class").indexOf("mode3")!=-1||
		($(".music_mode").attr("class").indexOf("mode1")!=-1&&
		player.nowindex>=player.musicList.length-1)
		){
			player.audio.currentTime=0
			player.audio.play();
			$(".song_geci").animate({marginTop:0},200)
		}
		else{
			$(".list_music").eq(player.nextindex()).find(".list_menu_play").trigger("click")
		}
		
	})
	
	$("body").delegate(".list_time a","click",function(){
		var music_now=$(this).parents(".list_music")
		if(music_now.get(0).index==player.nowindex){
			// $(".list_menu_play").eq(player.nowindex).trigger("click")
			$(".music_next").trigger("click")
		}
		music_now.remove()
		player.delmusic(music_now.get(0).index)
		
		$(".list_music").each(function(index,ele){
			ele.index=index
			$(ele).find(".list_index").text(index+1)
		})
	})
	
	$(".voice_biao").click(function(){		//直接静音的图标(biao意为图标的“biao”)
		$(this).toggleClass("voice_biao_xuan")
		if($(this).attr("class").indexOf("voice_biao_xuan")!=-1){		//同之前选中播放按钮一样，由于上一行代码作用，此处判断条件与预期相反
			player.voice_click(0)
		}
		else{
			voice_jindu.jindu_click(function(rate){
				player.voice_click(rate)
			})
			// player.voice_click(1)
		}
	})
	
	

	
	mode_time=0
	$(".music_mode").click(function(){
		add_class="mode"+(mode_time+1)%4
		if(add_class=="mode0"){
			$(".music_mode").attr("title","列表循环")
		}
		if(add_class=="mode1"){
			$(".music_mode").attr("title","顺序播放")
		}
		if(add_class=="mode2"){
			$(".music_mode").attr("title","随机播放")
		}
		if(add_class=="mode3"){
			$(".music_mode").attr("title","单曲循环")
		}
		remove_class="mode"+mode_time%4
		$(".music_mode").removeClass(remove_class)
		$(".music_mode").addClass(add_class)
		mode_time++
	})
	
	 function song(index,ele){
		 createsong=$(
        "<li class=\"list_music\">\n" +
            "<div class=\"list_check\"><i></i></div>\n" +
            "<div class=\"list_index\">"+(index + 1)+"</div>\n" +
            "<div class=\"list_name\">"+ele.name+
            "     <div class=\"list_menu\">\n" +
            "          <a href=\"javascript:;\" title=\"播放\" class='list_menu_play'></a>\n" +
            "          <a href=\"javascript:;\" title=\"添加\"></a>\n" +
            "          <a href=\"javascript:;\" title=\"下载\"></a>\n" +
            "          <a href=\"javascript:;\" title=\"分享\"></a>\n" +
            "     </div>\n" +
            "</div>\n" +
            "<div class=\"list_singer\">"+ele.singer+"</div>\n" +
            "<div class=\"list_time\">\n" +
            "     <span>"+ele.time+"</span>\n" +
            "     <a href=\"javascript:;\" title=\"删除\" ></a>\n" +
            "</div>\n" +
        "</li>");
		createsong.get(0).index=index    // get(0)将jq获取的元素转成js原生的元素
		createsong.get(0).music=ele
		// console.log(createsong);
		return createsong
	 }
})